package cn.org.dian.easycommunicate;

import cn.org.dian.easycommunicate.util.Constants;
import cn.org.dian.easycommunicate.util.NetworkTrafficStats;
import cn.org.dian.easycommunicate.util.OnlineTTS;
import cn.org.dian.easycommunicate.util.Utilities;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;

public class ConfigActivity extends PreferenceActivity {

    
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.config);
        // enable or disable some pref
        
                
        // init networkStats
        Preference networkStatsPref = findPreference(Constants.PREF_KEY_SHOW_MOBILE_NETWORK_STATS);
        String summaryToSet = null;
        if (NetworkTrafficStats.getAvailbility()) {
            // if stats available
            long statsInByte = Utilities.getSharedPreferences().getLong(Constants.PREF_KEY_MOBILE_NETWORK_STATS, 0);
            summaryToSet = getResources().getString(
                    R.string.show_mobile_network_stats_summary,
                    convertByteToMB(statsInByte));
        } else {
            summaryToSet = getResources().getString(R.string.network_stats_not_support);
            findPreference(Constants.PREF_KEY_SHOW_CLEAR_STAT).setEnabled(false);
        }
        networkStatsPref.setSummary(summaryToSet);
        
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference) {
        String key = preference.getKey();
        if (key.equals(Constants.PREF_KEY_SHOW_CLEAR_STAT)) {
            showClearStatsDialog();
        }
        
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }
    
        
    private void showClearStatsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.clear_stats_prompt)
            .setPositiveButton(R.string.confirm, 
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Utilities.getSharedPreferences().edit().putLong(
                            Constants.PREF_KEY_MOBILE_NETWORK_STATS, 0).commit();
                    Utilities.showShortToast(ConfigActivity.this, R.string.clear_success);
                }
            })
            .setNegativeButton(R.string.cancel, 
                    new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
        builder.create().show();
    }
    
    /**
     * 
     * @param bytes size in byte
     * @return size in MB like: 1.234M
     */
    private String convertByteToMB(long bytes) {
        bytes /= 1000; // total in KB
        String kbPart = String.valueOf(bytes % 1000);
        if (kbPart.length() == 1) {
            kbPart = "00" + kbPart;
        } else if (kbPart.length() == 2) {
            kbPart = "0" + kbPart;
        }
        String mbPart = String.valueOf(bytes / 1000);
        return mbPart + "." + kbPart + "M";
    }
}
